﻿using Set_File_Rw;
using System;
using System.IO;
using System.IO.Ports;
using System.Threading;
using TestFireBroadcastingHostPhone.Model;
using TestFireBroadcastingHostPhone.ViewModel;

namespace TestFireBroadcastingHostPhone.Product
{
    public class FunProduct :Productions
    {
        /// <summary>
        /// 产品串口打开的标志
        /// </summary>
        public bool isProductSerialPortOpen = false;
        /// <summary>
        /// 按键状态枚举
        /// </summary>
        public enum Key_Status : byte
        {
            /// <summary>
            /// 有效的
            /// </summary>
            Invalid = 0,
            /// <summary>
            /// 无效的
            /// </summary>
            Effective = 1,
        }
        /// <summary>
        /// 应急模式
        /// </summary>
        public enum EmrgencyMode : byte
        {
            /// <summary>
            /// 手动模式
            /// </summary>
            Manual = 0,
            /// <summary>
            /// 自动模式
            /// </summary>
            Auto = 1,
        }
        /// <summary>
        /// 录音输出模式
        /// </summary>
        public enum RedioMode : byte
        {
            /// <summary>
            /// 无模式
            /// </summary>
            Null = 0,
            /// <summary>
            /// 话筒广播
            /// </summary>
            MicroPhone = 1,
            /// <summary>
            /// 预录语音
            /// </summary>
            Video = 2,
        }

        /// <summary>
        /// 主电故障
        /// </summary>
        public bool Error_HostPower;
        /// <summary>
        /// 备电故障
        /// </summary>
        public bool Error_StandbyPower;
        /// <summary>
        /// 输出故障
        /// </summary>
        public bool Error_Output;
        /// <summary>
        /// 通讯故障
        /// </summary>
        public bool Error_Communicate;
        /// <summary>
        /// 话筒故障
        /// </summary>
        public bool Error_MicroPhone;
        /// <summary>
        /// 按键键值字节
        /// </summary>
        public byte[] KeyNumber = new byte[2];
        /// <summary>
        /// 面板上的按键键值
        /// </summary>
        public Key_Status[] Key_Value = new Key_Status[15];
        /// <summary>
        /// 产品应急模式
        /// </summary>
        public EmrgencyMode productEmrgencyMode;
        /// <summary>
        /// 产品录音输出模式
        /// </summary>
        public RedioMode productRedioMode = RedioMode.Null;
        /// <summary>
        /// 打开产品串口
        /// </summary>
        public void Open_CommunicatePort(out string err_info,int selectProductTypeIndex,string serialPortTTL = "", string serialPort485 = "")
        {
            err_info = string.Empty;
            string default_file_path = Directory.GetCurrentDirectory() + "\\RecordData\\RelatedRecord.ini";
            if (selectProductTypeIndex == (int)DefineTestData.StyleConfig.Panel_Need485 || selectProductTypeIndex == (int)DefineTestData.StyleConfig.Wall_IG8350Z
                || selectProductTypeIndex == (int)DefineTestData.StyleConfig.HostPhoneNeed485Reset)
            {
                if (!string.IsNullOrEmpty(serialPort485))
                {
                    err_info = string.Empty;
                    try
                    {
                        CommunicatePort = new SerialPort();              // 初始化串口对象
                        CommunicatePort.PortName = serialPort485;        // 设置串口名称
                        CommunicatePort.BaudRate = product_baudrate;     // 设置波特率
                        CommunicatePort.Parity = Parity.None;            // 设置校验位
                        CommunicatePort.DataBits = 8;                    // 设置数据位
                        CommunicatePort.StopBits = StopBits.One;         // 设置停止位
                        CommunicatePort.Open();                          // 打开串口
                        Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "485串口", serialPort485, out err_info);
                        isProductSerialPortOpen = true;
                    }
                    catch (Exception ex)
                    {
                        err_info = "Error opening serial port: " + ex.Message;// 处理异常
                    }
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(serialPortTTL))
                {
                    err_info = string.Empty;
                    try
                    {
                        CommunicatePort = new SerialPort();              // 初始化串口对象
                        CommunicatePort.PortName = serialPortTTL;             // 设置串口名称
                        CommunicatePort.BaudRate = product_baudrate;     // 设置波特率
                        CommunicatePort.Parity = Parity.None;            // 设置校验位
                        CommunicatePort.DataBits = 8;                    // 设置数据位
                        CommunicatePort.StopBits = StopBits.One;         // 设置停止位
                        CommunicatePort.Open();                          // 打开串口
                        Set_Data_Rw.Write(SystemConfigMV.configpath, default_file_path, "产品串口", serialPortTTL, out err_info);
                        isProductSerialPortOpen = true;
                    }
                    catch (Exception ex)
                    {
                        err_info = "Error opening serial port: " + ex.Message;// 处理异常
                    }
                }
            }
        }
        /// <summary>
        /// 关闭产品串口
        /// </summary>
        /// <param name="err_info">错误信息</param>
        public void Close_CommunicatePort(out string err_info)
        {
            err_info = string.Empty;
            try
            {
                if (CommunicatePort != null)
                {
                    CommunicatePort.Close();
                    CommunicatePort.Dispose();
                    CommunicatePort = null;
                    isProductSerialPortOpen = false;
                }
            }
            catch (Exception ex)
            {
                err_info = "Error closing serial port: " + ex.Message;// 处理异常
            }
        }
        /// <summary>
        /// 查询自动应急模式
        /// </summary>
        /// <param name="port">串口</param>
        /// <param name="err_info">错误信息</param>
        public void QueryAutoEmgencyMode(out string err_info)
        {
            err_info = string.Empty;
            byte[] receivebytes = new byte[8];
            if (CommunicatePort != null)
            {
                for (int i = 0; i < 3; i++)
                {
                    SendData(Display_vQueryAutoMode(), out err_info);
                    if (string.IsNullOrEmpty(err_info))
                    {
                        receivebytes = Receive_Wait(500, receivebytes.Length, out err_info);
                        if (string.IsNullOrEmpty(err_info) && receivebytes[1] == 0x0C)
                        {
                            if ((receivebytes[4] & 0x80) == 0)
                            {
                                productEmrgencyMode = EmrgencyMode.Manual;
                            }
                            else
                            {
                                productEmrgencyMode = EmrgencyMode.Auto;
                            }
                            break;
                        }
                    }
                    else
                    {
                        err_info += "发送数据错误";
                        break;
                    }
                }
            }
        }
        /// <summary>
        /// 查询录音模式
        /// </summary>
        /// <param name="err_info">错误信息</param>
        public void QueryRedioMode(out string err_info)
        {
            err_info = string.Empty;
            byte[] receivebytes = new byte[8];
            if (CommunicatePort != null)
            {
                for (int i = 0; i < 3; i++)
                {
                    SendData(Display_vQueryAutoMode(), out err_info);
                    if (string.IsNullOrEmpty(err_info))
                    {
                        receivebytes = Receive_Wait(500, receivebytes.Length, out err_info);
                        if (string.IsNullOrEmpty(err_info) && receivebytes[1] == 0x0C)
                        {
                            if ((receivebytes[5] & 0x01) == 0)
                            {
                                productRedioMode = RedioMode.MicroPhone;
                            }
                            else
                            {
                                productRedioMode = RedioMode.Video;
                            }
                            break;
                        }
                    }
                    else
                    {
                        err_info += "发送数据错误";
                        break;
                    }
                }
            }
        }
        /// <summary>
        /// 进入播放音频界面
        /// </summary>
        /// <param name="port">串口</param>
        /// <param name="err_info">错误信息</param>
        public void EnterDispalyVideosInterface(out string err_info)
        {
            err_info = string.Empty;
            byte[] receivebytes = new byte[7];
            if (CommunicatePort != null)
            {
                for (int i = 0; i < 3; i++)
                {
                    SendData(Display_vDisplayVideos(0xff, false), out err_info);//进入播放音频界面
                    if (string.IsNullOrEmpty(err_info))
                    {
                        receivebytes = Receive_Wait(1000, receivebytes.Length, out err_info);
                        if (string.IsNullOrEmpty(err_info)) break;
                    }
                    else
                    {
                        err_info += "发送数据错误";
                        break;
                    }
                }
            }
        }
        /// <summary>
        /// 播放U盘音频文件
        /// </summary>
        /// <param name="port">串口</param>
        /// <param name="err_info">错误信息</param>
        /// <param name="path">文件位置</param>
        /// <param name="status">播放状态</param>
        public void DisplayVideos( out string err_info, byte path, bool status)
        {
            err_info = string.Empty;
            byte[] receivebytes = new byte[7];
            if (CommunicatePort != null)
            {
                for (int i = 0; i < 3; i++)
                {
                    SendData(Display_vDisplayVideos(path, status), out err_info);
                    if (string.IsNullOrEmpty(err_info))
                    {
                        receivebytes = Receive_Wait(1000, receivebytes.Length, out err_info);
                        if (string.IsNullOrEmpty(err_info)) break;
                    }
                    else
                    {
                        err_info += "发送数据错误";
                        break;
                    }
                }
            }
        }
        /// <summary>
        /// 播放U盘音频文件
        /// </summary>
        /// <param name="err_info">错误信息</param>
        /// <param name="volume">音量大小</param>
        /// <param name="path">音频路径</param>
        /// <param name="status">音频状态</param>
        public void Function_DisplayVideos(out string err_info, byte path, bool status)
        {
            err_info = string.Empty;
            EnterDispalyVideosInterface(out err_info);//进入音频播放界面
            if (!string.IsNullOrEmpty(err_info))
            {
                err_info += "进入音频播放界面失败,错误信息：";
                return;
            }
            DisplayVideos(out err_info, path, status);//播放选择的音频
            if (!string.IsNullOrEmpty(err_info))
            {
                err_info += "播放音频失败,错误信息：";
            }
        }
        /// <summary>
        /// 查询功放状态
        /// </summary>
        /// <param name="port">串口号</param>
        /// <param name="address">功放地址</param>
        /// <param name="err_info">错误信息</param>
        public void QureyAmplifierStatus( byte address, out string err_info)
        {
            err_info = string.Empty;
            byte[] receivebytes = new byte[7];
            byte status = 0;
            if (CommunicatePort != null)
            {
                for (int i = 0; i < 3; i++)
                {
                    SendData(Display_vQueryAmplifier(address), out err_info);
                    if (string.IsNullOrEmpty(err_info))
                    {
                        receivebytes = Receive_Wait(500, receivebytes.Length, out err_info);
                        if (string.IsNullOrEmpty(err_info) && receivebytes[1] == 0x15)
                        {
                            status = receivebytes[4];//得到功放状态
                            Error_HostPower = BitSet(status, 0);
                            Error_StandbyPower = BitSet(status, 1);
                            Error_Output = BitSet(status, 2);
                            Error_Communicate = BitSet(status, 3);
                            break;
                        }
                    }
                    else
                    {
                        err_info += "发送数据错误";
                        break;
                    }
                }
            }
        }
        /// <summary>
        /// 功放自检
        /// </summary>
        /// <param name="port"></param>
        /// <param name="err_info"></param>
        /// <param name="time"></param>
        public void CheckSelf(out string err_info, byte time)
        {
            err_info = string.Empty;
            byte[] receivebytes = new byte[7];
            if (CommunicatePort != null)
            {
                for (int i = 0; i < 3; i++)
                {
                    SendData(Display_vCheckSelf(time), out err_info);
                    if (string.IsNullOrEmpty(err_info))
                    {
                        receivebytes = Receive_Wait(500, receivebytes.Length, out err_info);
                        if (string.IsNullOrEmpty(err_info)) break;
                    }
                    else
                    {
                        err_info += "发送数据错误";
                        break;
                    }
                }
            }
        }
        /// <summary>
        /// 注册功放
        /// </summary>
        /// <param name="port">串口</param>
        /// <param name="number">注册功放的数量</param>
        /// <param name="err_info">错误信息</param>
        public void RegisterAmplifier(byte number, out string err_info)
        {
            err_info = string.Empty;
            byte[] receivebytes = new byte[7];
            if (CommunicatePort != null)
            {
                for (int i = 0; i < 3; i++)
                {
                    SendData(Display_vRegisterAmplifier(number), out err_info);
                    if (string.IsNullOrEmpty(err_info))
                    {
                        receivebytes = Receive_Wait(500, receivebytes.Length, out err_info);
                        if (string.IsNullOrEmpty(err_info)) break;
                    }
                    else
                    {
                        err_info += "发送数据错误";
                        break;
                    }
                }
            }
        }
        /// <summary>
        /// 手动应急
        /// </summary>
        /// <param name="port">串口</param>
        /// <param name="err_info">错误信息</param>
        /// <param name="status">状态：启动/停止/暂停/继续</param>
        public void Manual_Emergency( out string err_info, Productions.EmergencyStatus status)
        {
            err_info = string.Empty;
            byte[] receivebytes = new byte[7];
            if (CommunicatePort != null)
            {
                for (int i = 0; i < 3; i++)
                {
                    SendData(Display_vEmergency_Control(status), out err_info);
                    if (string.IsNullOrEmpty(err_info))
                    {
                        receivebytes = Receive_Wait(500, receivebytes.Length, out err_info);
                        if (string.IsNullOrEmpty(err_info)) break;
                    }
                    else
                    {
                        err_info += "发送数据错误";
                        break;
                    }
                }
            }
        }
        /// <summary>
        /// 按键检测
        /// </summary>
        /// <param name="port">串口</param>
        /// <param name="err_info">错误信息</param>
        public void KeyCheck( out string err_info)
        {
            err_info = string.Empty;
            byte[] receivebytes = new byte[8];
            if (CommunicatePort != null)
            {
                for (int i = 0; i < 3; i++)
                {
                    SendData(Display_vKeyCheck(), out err_info);
                    if (string.IsNullOrEmpty(err_info))
                    {
                        receivebytes = Receive_Wait(500, receivebytes.Length, out err_info);
                        if (string.IsNullOrEmpty(err_info) && receivebytes[1] == 0x23)
                        {
                            KeyNumber[0] = receivebytes[5];
                            KeyNumber[1] = receivebytes[4];
                            break;
                        }
                    }
                    else
                    {
                        err_info += "发送数据错误";
                        break;
                    }
                }
            }
        }
        /// <summary>
        /// 主机复位
        /// </summary>
        /// <param name="port">串口</param>
        /// <param name="err_info">错误信息</param>
        public void HostPhoneReset(out string err_info)
        {
            err_info = string.Empty;
            byte[] receivebytes = new byte[7];
            if (CommunicatePort != null)
            {
                for (int i = 0; i < 3; i++)
                {
                    SendData(DisPlay_vHostPhoneReset(), out err_info);
                    if (string.IsNullOrEmpty(err_info))
                    {
                        receivebytes = Receive_Wait(500, receivebytes.Length, out err_info);
                        if (string.IsNullOrEmpty(err_info)) break;
                    }
                    else
                    {
                        err_info += "发送数据错误";
                        break;
                    }
                }
            }
        }
        /// <summary>
        /// 消音控制
        /// </summary>
        /// <param name="port">串口</param>
        /// <param name="err_info">错误信息</param>
        public void Silence_Control( out string err_info)
        {
            err_info = string.Empty;
            byte[] receivebytes = new byte[7];
            if (CommunicatePort != null)
            {
                for (int i = 0; i < 3; i++)
                {
                    SendData(Display_vSilence_Control(), out err_info);
                    if (string.IsNullOrEmpty(err_info))
                    {
                        receivebytes = Receive_Wait(500, receivebytes.Length, out err_info);
                        if (string.IsNullOrEmpty(err_info)) break;
                    }
                    else
                    {
                        err_info += "发送数据错误";
                        break;
                    }
                }
            }
        }
        /// <summary>
        /// 屏蔽注册功放
        /// </summary>
        /// <param name="port">串口</param>
        /// <param name="err_info">错误信息</param>
        public void Shield_RegisterAmplifier( byte status, out string err_info)
        {
            err_info = string.Empty;
            byte[] receivebytes = new byte[7];
            if (CommunicatePort != null)
            {
                for (int i = 0; i < 3; i++)
                {
                    SendData(Shield_RegisterAmplifier(status), out err_info);
                    if (string.IsNullOrEmpty(err_info))
                    {
                        receivebytes = Receive_Wait(500, receivebytes.Length, out err_info);
                        if (string.IsNullOrEmpty(err_info)) break;
                    }
                    else
                    {
                        err_info += "发送数据错误";
                        break;
                    }
                }
            }
        }
        /// <summary>
        /// 测试命令配置
        /// </summary>
        /// <param name="port">串口</param>
        /// <param name="status">命令</param>
        /// <param name="err_info">错误信息</param>
        public void Cmd_TestConfig( byte status, out string err_info)
        {
            err_info = string.Empty;
            byte[] receivebytes = new byte[7];
            if (CommunicatePort != null)
            {
                for (int i = 0; i < 3; i++)
                {
                    SendData(Display_vShieldAreaContorlDisplay(status), out err_info);
                    if (string.IsNullOrEmpty(err_info))
                    {
                        receivebytes = Receive_Wait(500, receivebytes.Length, out err_info);
                        if (string.IsNullOrEmpty(err_info)) break;
                    }
                    else
                    {
                        err_info += "发送数据错误";
                        break;
                    }
                }
            }
        }
        /// <summary>
        /// byte有效位转换bool
        /// </summary>
        /// <param name="number">输入字节</param>
        /// <param name="position">有效位索引</param>
        /// <returns>bool值</returns>
        public static bool BitSet(byte number, int position)
        {
            return (number & (1 << position)) != 0;
        }
        /// <summary>
        /// 刷字库，将应急音源更换
        /// </summary>
        /// <param name="port">串口</param>
        /// <param name="err_info">错误信息</param>
        /// <param name="bin_filePath">音源板文件</param>
        public void Function_SplitIntoFrames(out string err_info,out bool result, string bin_filePath)
        {
            err_info = string.Empty;
            result = false;
            byte[] receivebytes = new byte[7];
            if (CommunicatePort is null) return;
            SendData(Dispaly_vResetCommunicateBaudRate(4), out err_info);//将下位机改成115200的波特率
            if (!string.IsNullOrEmpty(err_info)) return;
            if (CommunicatePort != null)
            {
                CommunicatePort.Close();
                Thread.Sleep(50);
                CommunicatePort.BaudRate = 115200;
                CommunicatePort.Open();
            }
            SendData(Display_vEnterUpdateVoiceSource(), out err_info);//先进入音源板刷字库模式
            if (!string.IsNullOrEmpty(err_info)) return;
            receivebytes = Receive_Wait(500, receivebytes.Length, out err_info);
            if (!string.IsNullOrEmpty(err_info)) return;
            Thread.Sleep(50);
            byte[] fileBytes = File.ReadAllBytes(bin_filePath);
            int totalFrames = (int)Math.Ceiling((double)fileBytes.Length / 224);
            byte[] payload = new byte[224]; // 只需要创建一次，用于临时存储每一帧
            byte[] sendData;
            for (int i = 0; i < totalFrames; i++)
            {
                int start = i * 224;
                int length = Math.Min(224, fileBytes.Length - start);
                Array.Copy(fileBytes, start, payload, 0, length);
                // 如果是最后一帧并且长度不足224字节，填充剩余部分
                if (i == totalFrames - 1 && length < 224)
                {
                    sendData = Dispaly_vUpdateVoiceSource(payload, length);// 直接使用实际长度的数据发送
                }
                else
                {
                    sendData = Dispaly_vUpdateVoiceSource(payload);// 正常发送帧
                }
                SendData(sendData, out err_info);
                if (!string.IsNullOrEmpty(err_info))
                {
                    err_info += "数据发送错误";
                    result = false;
                    break;
                }
                receivebytes = Receive_Wait(500, receivebytes.Length, out err_info);
                if (!string.IsNullOrEmpty(err_info))
                {
                    err_info += "数据接收错误";
                    result = false;
                    break;
                }
            }
            SendData(Display_vExitUpdateVoiceSource(), out err_info);//最后退出音源板刷字库模式
            if (!string.IsNullOrEmpty(err_info)) return;
            receivebytes = Receive_Wait(500, receivebytes.Length, out err_info);
            if (!string.IsNullOrEmpty(err_info)) return;
            Thread.Sleep(50);
            result = true;
            SendData(Dispaly_vResetCommunicateBaudRate(0), out err_info);//将下位机改回4800的波特率
            if (!string.IsNullOrEmpty(err_info)) return;
            if (CommunicatePort != null)
            {
                CommunicatePort.Close();
                Thread.Sleep(50);
                CommunicatePort.BaudRate = 4800;
                CommunicatePort.Open();
            }
        }
        /// <summary>
        /// 设置音源板地址
        /// </summary>
        /// <param name="port">串口</param>
        /// <param name="err_info">错误信息</param>
        /// <param name="address">地址1-255</param>
        public void Set_AmplifierAddress( out string err_info, byte address)
        {
            err_info = string.Empty;
            byte[] receivebytes = new byte[7];
            if (CommunicatePort != null)
            {
                for (int i = 0; i < 3; i++)
                {
                    SendData(Display_vSet_AmplifierAddress(address), out err_info);
                    if (string.IsNullOrEmpty(err_info))
                    {
                        receivebytes = Receive_Wait(500, receivebytes.Length, out err_info);
                        if (string.IsNullOrEmpty(err_info)) break;
                    }
                    else
                    {
                        err_info += "发送数据错误";
                        break;
                    }
                }
            }
        }
        /// <summary>
        /// 查询音源板地址
        /// </summary>
        /// <param name="port">串口号</param>
        /// <param name="err_info">错误信息</param>
        public byte Qurey_AmplifierAddress( out string err_info)
        {
            err_info = string.Empty;
            byte address = 0;
            byte[] receivebytes = new byte[7];
            if (CommunicatePort != null)
            {
                for (int i = 0; i < 3; i++)
                {
                    SendData(Display_vQuery_AmplifierAddress(), out err_info);
                    if (string.IsNullOrEmpty(err_info))
                    {
                        receivebytes = Receive_Wait(500, receivebytes.Length, out err_info);
                        if (string.IsNullOrEmpty(err_info))
                        {
                            address = receivebytes[4];
                            break;
                        }
                    }
                    else
                    {
                        err_info += "发送数据错误";
                        break;
                    }
                }
            }
            return address;
        }
        /// <summary>
        /// 播放录音
        /// </summary>
        /// <param name="port">串口号</param>
        /// <param name="sw">播放开关</param>
        /// <param name="err_info">错误信息</param>
        public void Function_Display_Redio( byte sw, out string err_info)
        {
            err_info = string.Empty;
            byte[] receivebytes = new byte[7];
            if (CommunicatePort != null)
            {
                for (int i = 0; i < 3; i++)
                {
                    SendData(Dispaly_vDisplay_Redio(sw), out err_info);
                    if (string.IsNullOrEmpty(err_info))
                    {
                        receivebytes = Receive_Wait(500, receivebytes.Length, out err_info);
                        if (string.IsNullOrEmpty(err_info))
                        {
                            if (receivebytes[4] == 0xFF)
                            {
                                err_info = "当前无录音!";
                            }
                            break;
                        }
                    }
                    else
                    {
                        err_info += "发送数据错误";
                        break;
                    }
                }
            }
        }
        /// <summary>
        /// 删除录音
        /// </summary>
        /// <param name="port">串口号</param>
        /// <param name="err_info">错误信息</param>
        public void Function_Delete_Redio( out string err_info)
        {
            err_info = string.Empty;
            byte[] receivebytes = new byte[7];
            if (CommunicatePort != null)
            {
                for (int i = 0; i < 3; i++)
                {
                    SendData(Dispaly_vDelete_Redio(), out err_info);
                    if (string.IsNullOrEmpty(err_info))
                    {
                        receivebytes = Receive_Wait(500, receivebytes.Length, out err_info);
                        if (string.IsNullOrEmpty(err_info)) break;
                    }
                    else
                    {
                        err_info += "发送数据错误";
                        break;
                    }
                }
            }
        }
        /// <summary>
        /// 调节监听音量
        /// </summary>
        /// <param name="port">串口号</param>
        /// <param name="sw">调节开关</param>
        /// <param name="err_info">错误信息</param>
        public void Function_Adjust_MornitorVolume(byte sw, out string err_info)
        {
            err_info = string.Empty;
            byte[] receivebytes = new byte[7];
            if (CommunicatePort != null)
            {
                for (int i = 0; i < 3; i++)
                {
                    SendData(Dispaly_Adjust_MornitorVolume(sw), out err_info);
                    if (string.IsNullOrEmpty(err_info))
                    {
                        receivebytes = Receive_Wait(500, receivebytes.Length, out err_info);
                        if (string.IsNullOrEmpty(err_info)) break;
                    }
                    else
                    {
                        err_info += "发送数据错误";
                        break;
                    }
                }
            }
        }
        /// <summary>
        /// 查询话筒状态
        /// </summary>
        /// <param name="port">串口号</param>
        /// <param name="err_info">错误信息</param>
        public void Function_QueryMicroStatus( out string err_info)
        {
            err_info = string.Empty;
            byte status = 0;
            byte[] receivebytes = new byte[7];
            if (CommunicatePort != null)
            {
                for (int i = 0; i < 3; i++)
                {
                    SendData(Dispaly_QueryMicroPhoneStatus(), out err_info);
                    if (string.IsNullOrEmpty(err_info))
                    {
                        receivebytes = Receive_Wait(500, receivebytes.Length, out err_info);
                        if (string.IsNullOrEmpty(err_info) && receivebytes[1] == 0x27)
                        {
                            status = receivebytes[4];//得到功放状态
                            Error_MicroPhone = BitSet(status, 0);
                            break;
                        }
                    }
                    else
                    {
                        err_info += "发送数据错误";
                        break;
                    }
                }
            }
        }
    }
}
